import React from 'react';
import { Tooltip } from 'antd';
import { translateT2S } from '@/apis';
import { useState } from 'react';

/**
 * fix log text due to space
 * @param text {string}
 * @param length {number}
 */
const fixLongText = (text: string, length?: number) => {
  const fixLength: number = length || 10;
  // const [rawText, setRawText] = useState(text);
  if (text.length > fixLength) {
    // translateT2S(text).then((res) => {
    //   setRawText(res.data);
    // });
    return (
      <>
        <Tooltip
          placement="topLeft"
          title={
            <p>
              {text} <b>转简体</b>
            </p>
          }
        >
          <p>{text.substring(0, fixLength)}...</p>
        </Tooltip>
      </>
    );
  } else {
    return (
      <>
        <Tooltip
          placement="topLeft"
          title={
            <p>
              {text} <b>转简体</b>
            </p>
          }
        >
          <p>{text}</p>
        </Tooltip>
      </>
    );
  }
};

/**
 * fixNullData
 * @param text {string}
 * @return string
 */
const fixNullData = (text?: string): string => {
  if (typeof text === undefined) {
    return '暂无数据';
  }
  if (text === '' || text === 'nan') {
    return '暂无数据';
  }
  return text as string;
};

/**
 *
 * @param majorName
 */
const checkMajorNameNullOrUndefined = (majorName?: string): boolean => {
  if (typeof majorName === undefined) {
    return false;
  }
  if (majorName === '') {
    return false;
  }
  return true;
};

/**
 * fixNullPage
 * @param page {string}
 * @return number
 */
const fixNullPage = (page?: string): string => {
  if (typeof page === undefined) {
    return '暂无数据';
  }
  if (page === '' || page === 'nan') {
    return '暂无数据';
  }
  return JSON.stringify(parseInt(page as string));
};

export { fixLongText, fixNullData, checkMajorNameNullOrUndefined, fixNullPage };
